package com.tiyende.credit.loop.ui.activity

import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.lib.common.adapter.CommonOnItemChildClickListener
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.common.CommonActivity
import com.tiyende.credit.loop.databinding.TiyendeActivityOrderListBinding
import com.tiyende.credit.loop.event.TiyendeEventPointType
import com.tiyende.credit.loop.event.TiyendeEventData
import com.tiyende.credit.loop.ui.adapter.TiyendeOrderListAdapter
import com.tiyende.credit.loop.utils.TiyendeDensityUtil
import com.tiyende.credit.loop.utils.decoration.LinearSpacingItemDecoration
import com.tiyende.credit.loop.utils.helper.TiyendeOrderHelper
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/12/18 17:51
 * @Annotation：订单列表
 */
class TiyendeOrderListActivity: CommonActivity(){
    lateinit var binding: TiyendeActivityOrderListBinding
    private var orderAdapter: TiyendeOrderListAdapter? = null
    //显示类型
    private var showOrderType = "InProgress"
    override fun getTiyendeRootView(): View? {
        binding = TiyendeActivityOrderListBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initTiyendeView() {
        setTiyendeTitleBarVisibility(View.VISIBLE)
        setTiyendeTextTitleVisibility(View.GONE)
        setTiyendeBackBtnRes(R.mipmap.tiyende_back_white)
        setTiyendeRightBtnRes(R.mipmap.tiyende_customer_white)
        initOrderListRefrashView()
        initOrderListView()
    }

    override fun initTiyendeListener() {
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("OrderList Page")
            addEventPointRequestAction(TiyendeEventPointType.CLICK.TIYENDE_CLICK_CUSTOMER,"OrderList Page")
        }

        //审核中、放款中
        binding.txtOrderInProgress.setOnClickListener {
            showOrderType = "InProgress"
            showTiyendeOrderTypeView()
        }

        //已拒绝
        binding.txtOrderRejected.setOnClickListener {
            showOrderType = "Rejected"
            showTiyendeOrderTypeView()
        }
    }

    override fun loadTiyendeData() {
        showOrderType = "InProgress"
        showTiyendeOrderTypeView()
    }

    /* 初始化刷新 */
    private fun initOrderListRefrashView() {
        binding.orderListRefreshView.setColorSchemeResources(R.color.theme,R.color.purple_500)
        binding.orderListRefreshView.setOnRefreshListener {
            publishEventData(TiyendeEventData(TiyendeEventData.TYPE.TIYENDE_ORDER_REFRESH))
        }
    }

    private fun initOrderListView() {
        binding.rvOrderList.layoutManager = LinearLayoutManager(this)
        orderAdapter = TiyendeOrderListAdapter(binding.rvOrderList)
        binding.rvOrderList.adapter = orderAdapter
        val leftDecoration = TiyendeDensityUtil.dp2px(16f)
        binding.rvOrderList.addItemDecoration(LinearSpacingItemDecoration(0, 0,leftDecoration,leftDecoration))
        binding.rvOrderList.itemAnimator = DefaultItemAnimator()
        orderAdapter?.setOnItemChildClickListener(CommonOnItemChildClickListener { _, childView, position ->
            val item = orderAdapter?.getItem(position)
            item?.let {
                when(childView.id){
                    R.id.btnOrderDetails -> {//查看详情
                        TiyendeOrderInfoActivity.start(this,it.optString("orderId"))
                    }
                }
            }
        })
    }

    /**
     * 展示订单类型
     */
    private fun showTiyendeOrderTypeView(){
        binding.txtOrderInProgress.setTextColor(Color.parseColor("#FF000000"))
        binding.imageOrderInProgress.visibility = View.INVISIBLE
        binding.txtOrderRejected.setTextColor(Color.parseColor("#FF000000"))
        binding.imageOrderRejected.visibility = View.INVISIBLE
        val otherList = TiyendeOrderHelper.queryTiyendeOrderOtherList()
        val orderList = arrayListOf<JSONObject>()
        when(showOrderType){
            "InProgress" -> {//审核中、放款中
                binding.txtOrderInProgress.setTextColor(Color.parseColor("#FFFFFFFF"))
                binding.imageOrderInProgress.visibility = View.VISIBLE
                otherList.forEach {
                    if ("REJECT" != it.optString("orderStatus")){
                        orderList.add(it)
                    }
                }
            }
            else -> {//已拒绝
                binding.txtOrderRejected.setTextColor(Color.parseColor("#FFFFFFFF"))
                binding.imageOrderRejected.visibility = View.VISIBLE
                otherList.forEach {
                    if ("REJECT" == it.optString("orderStatus")){
                        orderList.add(it)
                    }
                }
            }
        }

        if (orderList.size == 0){
            orderAdapter?.clear()
            val emptyItem = JSONObject()
            emptyItem.put("isEmptyView",true)
            orderAdapter?.addFirstItem(emptyItem)
            return
        }
        orderAdapter?.data = orderList
    }


    override fun onTiyendePublishEvent(event: TiyendeEventData) {
        super.onTiyendePublishEvent(event)
        when(event.eventType){
            TiyendeEventData.TYPE.TIYENDE_ORDER_REFRESHED -> {
                showTiyendeOrderTypeView()
            }
            TiyendeEventData.TYPE.TIYENDE_ORDER_FINISHED -> {
                hiddenTiyendeLoading()
            }
            else -> {

            }
        }
    }

    override fun hiddenTiyendeLoading() {
        super.hiddenTiyendeLoading()
        binding.orderListRefreshView.isRefreshing = false
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_ORDER_LIST_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_ORDER_LIST_END)
    }
}